const Service = require('egg').Service;
class PrizeService extends Service {

    // 奖品列表
    async list({ pageIndex = 0, pageSize = 10 }) {
        var pageIndex = pageIndex * 1 * pageSize;
        var pageSize = pageSize * 1;
        const options = {
            offset: pageIndex,
            limit: pageSize,
            attributes: ['id', 'name', 'types', 'num', 'usenum'],
            order: [['types', 'ASC']]
        };
        const prize = this.ctx.model.Prize.findAndCountAll(options);
        return prize;
    }

    // 界面详情
    async find(id) {
        const prize = await this.ctx.model.Prize.findById(id);
        if (!prize) {
            this.ctx.throw(500, "没有该奖品信息不存在");
        }
        return prize;
    }

    // 添加奖品
    async create(data) {
        console.log(typeof (data));
        const options = {
            where: {
                types: data.types
            }
        };
        const prize = await this.ctx.model.Prize.findOne(options);
        console.log(prize);
        if (prize) {
            this.ctx.throw(500, "奖品已存在");
        }
        return this.ctx.model.Prize.create(data);
    }
    // 编辑节目
    async update(data) {
        const prize = await this.ctx.model.Prize.findById(data.id);
        if (!prize) {
            this.ctx.throw(500, "更新失败");
        }
        return prize.updateAttributes(data);
    }

    // 删除节目
    async del(id) {
        const prize = await this.ctx.model.Prize.findById(id);
        if (!prize) {
            this.ctx.throw(500, "删除失败");
        }
        return prize.destroy();
    }
}
module.exports = PrizeService;